package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.interfaces.OnItemClickListener;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.presenter.MyChoosePresenter;
import com.finace.money.ui.adapter.StatusListViewAdapter;
import com.finace.money.ui.widget.CustomHScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/29.
 */

public class MyChooseActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;

    @BindView(R.id.h_scrollView)
    CustomHScrollView mScrollView;
    @BindView(R.id.list_view)
    ListView mListView;
    @BindView(R.id.content_main)
    RelativeLayout mHead;

    private List<StatusListBean> mDataList = new ArrayList<>();
    private StatusListViewAdapter mAdapter;
    private int leftPos;//用于记录CustomHScrollView的初始位置
    private int topPos;
    private List<StatusListBean> data;
    private String where;
    private MyChoosePresenter mMyChoosePresenter = new MyChoosePresenter();

    @Override
    public int setLayout() {
        return R.layout.activity_my_choose;
    }

    @Override
    public void init() {
        rlLeft.setOnClickListener(this);
        initList();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_TICK);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (null != intent) {
                TickBean tickBean = (TickBean) intent.getSerializableExtra(PushReceiver.DATA);
                for (StatusListBean statusListBean : data) {
                    if (statusListBean.getM_Body().getM_ProductID().equals(tickBean.getSymbol())) {
                        statusListBean.getM_Body().setTickbean(tickBean);
                        mAdapter.notifyDataSetChanged();
                        return;
                    }
                }
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;
        }
    }

    private void initList() {
        mHead.setFocusable(true);
        mHead.setClickable(true);
        mHead.setOnTouchListener(new MyTouchLinstener());
        mListView.setOnTouchListener(new MyTouchLinstener());
        initData();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                String login = ACache.get(UIUtil.getContext()).getAsString(Constant.login);
                if(StringUtil.isNullString(login)){
                    startActivity(new Intent(UIUtil.getContext(), LoginActivity.class));
                }else {
                    Intent intent = new Intent(UIUtil.getContext(), StatusDetalActivity.class);
                    intent.putExtra("statusData", mDataList.get(i));
                    startActivity(intent);
                }
                /*Intent intent = new Intent(UIUtil.getContext(), StatusDetalActivity.class);
                intent.putExtra("statusData", mDataList.get(i));
                UIUtil.startActivity(intent);*/
            }
        });
        mAdapter.setmOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                if (StringUtil.isNotNullString(where)) {
                    StatusListBean bean = mDataList.get(position);
                    mMyChoosePresenter.setChoose(data, bean);
                    mAdapter.notifyDataSetChanged();
                } else {
                    StatusListBean remove = mDataList.remove(position);
                    mMyChoosePresenter.removeChoose(data, remove);
                    mAdapter.notifyDataSetChanged();

                    if (mDataList.size() == 0) {
                        onBackPressed();
                    }
                }

            }
        });

    }

    class MyTouchLinstener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            //当在表头和listView控件上touch时，将事件分发给 ScrollView
            HorizontalScrollView headSrcrollView = (HorizontalScrollView) mHead.findViewById(R.id.h_scrollView);
            headSrcrollView.onTouchEvent(arg1);
            return false;
        }
    }

    private void initData() {
        mDataList.clear();
        //区分主力合约和自选
        where = getIntent().getStringExtra("where");

        if (StringUtil.isNotNullString(where)) {
            data = (List<StatusListBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.ACTION_LIST);
            tvHeadTitle.setText(UIUtil.getString(R.string.importent));
        } else {
            data = (List<StatusListBean>) ACache.get(UIUtil.getContext()).getAsObject(Constant.CHOOSE_ALL);
            tvHeadTitle.setText(UIUtil.getString(R.string.my_choose));
        }
        if (null != data && data.size() > 0) {

            if (StringUtil.isNotNullString(where)) {
                for (StatusListBean bean : data) {
                    if (bean.getM_Body().getM_IsMain() == 1) {
                        mDataList.add(bean);
                    }
                }

            } else {
                mDataList.addAll(data);
            }
        }


        mAdapter = new StatusListViewAdapter(mHead);
        mListView.setAdapter(mAdapter);
        mAdapter.setmData(mDataList);
    }

    /**
     * 记录CustomHScrollView的初始位置
     *
     * @param l
     * @param t
     */
    public void setPosData(int l, int t) {
        this.leftPos = l;
        this.topPos = t;
    }

    @Override
    public void onBackPressed() {
        setResult(0, null);
        finish();
        super.onBackPressed();
    }
}
